$(document).ready(function(){
    $("#myform").validate();       
})

function doCA(target){
    if(target != 0){
        $.ajax({
            type: 'POST',
            url: SITE_URL+'/ca/' + target,
            data: '',
            success: function(data){
                $('#the_content').html(data);
                errorValue = Array();
            }
        })
    }
}

function doAcc(target,id){    
    var yearterm = $('#yearterm').val();
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/' + target,
        data: 'id='+id+'&yearterm='+yearterm,
        success: function(data){            
            $('#the_content').html(data);
        }
    })
}



function delete_item(id,target,table){
    var the_id = '#'+id;    
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/check_exist_table',
        data: 'id='+id+'&table='+table,
        success: function(data){   
            if(data == table){
                alert('This record has relatived with another table. Please check again!');
                return;
            }else{
                $.ajax({
                    type: 'POST',
                    url: SITE_URL+'/ca/'+target,
                    data: 'id='+id,
                    success: function(){                                    
                        $('input[key='+id+']').val('');
                        $(the_id).fadeOut();                                    
                    }
                }) 
            }
        }
    })

}

function validate_form(id){
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/validate_form',
        data: 'id='+id,
        success: function(data){            
            $('#the_content').html(data);
        }
    })
}

function changeYear(target){
    var yearterm = $('#yearterm').val();
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/' + target,
        data: 'yearterm='+yearterm,
        success: function(data){
            $('#the_content').html(data);
        }
    })
}

function changeForm(target){
    var yearterm = $('#yearterm').val();
    var formId = $('#changeform').val();        
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/' + target,
        data: 'yearterm='+yearterm+'&formId='+formId,
        success: function(data){
            $('#the_content').html(data);
        }
    })
}

function changeArea(target){
    var yearterm = $('#yearterm').val();
    var formId = $('#changeform').val();        
    var areaId = $('#changearea').val();        
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/' + target,
        data: 'yearterm='+yearterm+'&formId='+formId+'&areaId='+areaId,
        success: function(data){
            $('#the_content').html(data);
        }
    })
}

function changeQuestion(target){
    var yearterm = $('#yearterm').val();
    var formId = $('#changeform').val();        
    var areaId = $('#changearea').val();   
    var questionId = $('#changequestion').val();  
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/' + target,
        data: 'yearterm='+yearterm+'&formId='+formId+'&areaId='+areaId+'&questionId='+questionId,
        success: function(data){
            $('#the_content').html(data);
        }
    })
}

/*
 *@author Tri Nguyen
 *@function addNew
 *@param 
 *yearterm year and term
 *target url to process locate 
 *
 */

function addNew(target){
    var total = $('#total').val();     
    var id = $('#max_id').val();        
    var yearterm = $('#yearterm').val();
    var form = $('#changeform').val();
    var area = $('#changearea').val();
        if(form == 0 || area == 0){
            alert('Not enough data to add new');
            return;
        }
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/'+target,
        data: 'yearterm='+yearterm+'&total='+total+'&id='+id,
        success: function(data){
            //alert(data);            
            total = parseInt(total) + 1;
            id = parseInt(id) + 1;
            $('#mytable').append(data);   
            $('#total').val(total);            
            $('#max_id').val(id);                                                   
            $( ".datepicker" ).datepicker({
                dateFormat: 'yy-mm-dd',
                showAnim: 'slideDown'
            });                      
            $('#submit').removeClass('hidden');
        }
    })
}

function addNewArea(){
    var total = $('#total').val();
    var id = $('#max_id').val();  
    id = parseInt(id) + 1;
    $.ajax({
        type: 'POST',
        url: SITE_URL+'/ca/add_new_form',
        data: 'yearterm='+yearterm+'&total='+total+'&id='+id,
        success: function(data){
            total = parseInt(total) + 1;
            $('#mytable').append(data);   
            $('#total').val(total);            
            $('#max_id').val(id);             
        }
    }) 
}

function CheckExtenstion(con)
{
    if(con.value != "")
    {
        if(con.value.indexOf(".") > -1)
        {
            var ext = con.value.substring(con.value.lastIndexOf(".") + 1);            
            if(ext.toLowerCase() == "jpeg" || ext.toLowerCase() == "jpg" || ext.toLowerCase() == "gif" || ext.toLowerCase() == "tif" || ext.toLowerCase() == "tiff" || ext.toLowerCase() == "bmp" || ext.toLowerCase() == "png" || ext.toLowerCase() == "wmv" || ext.toLowerCase() == "avi" || ext.toLowerCase() == "mpeg" || ext.toLowerCase() == "mpg" || ext.toLowerCase() == "mp4" || ext.toLowerCase() == "flv")
            {
            /* do nothing */
            /* find a better way of checking the file type */
            }
            else
            {
                alert('Please select an image or video file only.');
                con.value="";
                con.focus();
            }
        }
        else
        {
            alert('Please enter valid file');
        }
    }
}   
var errorValue = Array();

function isValid(field){
    var check = true;
    if(field.value.length == 0){
        check = false;        
    }
    
    if (errorValue.indexOf(field) >= 0)
    {
        errorValue[errorValue.indexOf(field)] = errorValue[0];
        errorValue.shift();
    }
    //apply appropriate colour based on value
    if(!check)
    {
        field.style.backgroundColor = "red";
        errorValue.push(field);
    //field.value = "0";
    // alert("The money input is not validate");
    }
    else
    {
        field.style.backgroundColor = "white";
    }
    
}

function isMoney(field){
    var check = true;

    var ValidChars = "0123456789.";    
    var Char;
 
    for (i = 0; i < field.value.length && check == true; i++) 
    { 
        Char = field.value.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            check = false;
            break;
        }
    }
    
    if(field.value.length == 0){
        check = false;        
    }

    if (errorValue.indexOf(field) >= 0)
    {
        errorValue[errorValue.indexOf(field)] = errorValue[0];
        errorValue.shift();
    }
    //apply appropriate colour based on value
    if(!check)
    {
        field.style.backgroundColor = "red";
        errorValue.push(field);
    //field.value = "0";
    // alert("The money input is not validate");
    }
    else
    {
        field.style.backgroundColor = "white";
    }
}
	
function checkform()
{    
    if (errorValue.length >0)
    {
        alert("The input value are incorect.");
        errorValue[0].focus();
        return false;
    }
    return true;
}



